
//  CollectionViewCell.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "CollectionViewCell.h"
#import "ModelForData.h"
#import "FocusImageTableViewCell.h"
#import "XiaoBianTuiJianTableViewCell.h"
#import "HotRecommentTableViewCell.h"
#import <MJRefresh.h>


#import "NetworkHandlerBlock.h"


@interface CollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation CollectionViewCell

- (void)dealloc {

    [_tableView release];
    [_arrForTableViewOne release];
    [super dealloc];
}

// 初始化方法,只会走一遍.
- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

//        if (self.arrForTableViewOne) {
        
           [self createTableView];
            
//        }else {
//            UIImageView *backGround = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wuwang"]];
//            backGround.frame = self.bounds;
//            [self addSubview:backGround];
//            [backGround release];
//        }
        
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
        
    }
    return self;
}

#pragma mark createTableView
- (void)createTableView {

    self.tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[FocusImageTableViewCell class] forCellReuseIdentifier:@"poolForFocusImageTableViewCell"];
    [self.tableView registerClass:[XiaoBianTuiJianTableViewCell class] forCellReuseIdentifier:@"poolForXiaoBianTuiJianTableViewCell"];
    [self.tableView registerClass:[HotRecommentTableViewCell class] forCellReuseIdentifier:@"poolForHotRecommentTableViewCell"];
  
    [self.tableView release];
}

#pragma mark tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrForTableViewOne.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 

    ModelForData *model = [self.arrForTableViewOne objectAtIndex:indexPath.row];
 
    if (model.arrForPic) {
    // 创建cell.
    FocusImageTableViewCell *cellFocusImage = [tableView dequeueReusableCellWithIdentifier:@"poolForFocusImageTableViewCell"];
    // 赋值.
    [cellFocusImage passModelForDataToCellOne:model];

    return cellFocusImage;
    }

    if (model.arrForXiaoBian) {
    XiaoBianTuiJianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForXiaoBianTuiJianTableViewCell"];

    [cell passModelForData:model];
    return cell;   
    }
// if (model.arrForHotRecommends)
   else {
        HotRecommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForHotRecommentTableViewCell"];

        [cell passModelForData:model];
        return cell;
       }
}

#pragma mark tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForData *model = [self.arrForTableViewOne objectAtIndex:indexPath.row];
    if (model.arrForPic) {
        return WIDTH * 3/5;
    }
    return 60 + (WIDTH - 40)/3 *4/3 + 70;
}


- (void)loadNewData {
        
        [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-d10&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.44.2" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
            
            self.arrForTableViewOne = [NSMutableArray array];
            
            NSDictionary *dicTemp = [NSDictionary dictionaryWithDictionary:result];
            NSMutableArray *arrTemp = [NSMutableArray array];
            
//            if ([[dicTemp allKeys] containsObject:@"focusImages"] ) {
            
                
                [arrTemp addObject:[dicTemp objectForKey:@"focusImages"]];
                [arrTemp addObject:[dicTemp objectForKey:@"editorRecommendAlbums"]];
                [arrTemp addObject:[dicTemp objectForKey:@"hotRecommends"]];
                
                for (NSDictionary *dic in arrTemp) {
                    
                    if ([[dic objectForKey:@"title"] isEqualToString:@"小编推荐"]) {
                        ModelForData *model = [[ModelForData alloc]init];
                        model.arrForXiaoBian = [NSMutableArray arrayWithArray:[dic objectForKey:@"list"]];
                        [self.arrForTableViewOne addObject:model];
                        [model release];
                    }
                    
                    if ([[dic objectForKey:@"title"] isEqualToString:@"热门推荐"]) {
                        
                        // 最外层list,几乎包含所有信息.(包含外层大字典的信息,更包括大字典中三个小字典列表的信息)
                        NSArray *arrTemp = [NSArray arrayWithArray:[dic objectForKey:@"list"]];
                        
                        for (NSDictionary *dic in arrTemp) {
                            ModelForData *model = [[ModelForData alloc]init];
                            [model setValuesForKeysWithDictionary:dic];
                            
                            // 里层的list数组,包含三个小字典的信息.
                            model.arrForHotRecommends = [NSMutableArray arrayWithArray:[dic objectForKey:@"list"]];
                            
                            [self.arrForTableViewOne addObject:model];
                            [model release];
                        }
                    }
                    
                    if ([[dic objectForKey:@"title"] isEqualToString:@"焦点图"]) {
                        ModelForData *model = [[ModelForData alloc]init];
                        model.arrForPic = [NSMutableArray arrayWithArray:[dic objectForKey:@"list"]];
                        [self.arrForTableViewOne addObject:model];
                        [model release];
                    }
                }
            
            
            // 数据解析完毕之后再将collection刷新一遍.
            [self.tableView reloadData];
        }];
    
}






@end
